package vip.xiaonuo.sys.fymodule.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sys.fymodule.account.enums.CodeEnum;
import vip.xiaonuo.sys.fymodule.exception.BusinessException;
import vip.xiaonuo.sys.fymodule.order.entity.SxoPayment;
import vip.xiaonuo.sys.fymodule.order.mapper.SxoPaymentMapper;
import vip.xiaonuo.sys.fymodule.order.service.ISxoPaymentService;

/**
 * 支付方式 业务层实现
 *
 * @author wangy
 * @since 2025-06-20
 */
@Slf4j
@Service
public class SxoPaymentServiceImpl extends ServiceImpl<SxoPaymentMapper, SxoPayment> implements ISxoPaymentService {


    @Override
    public SxoPayment findPayConfigById(String paymentId) {
        if (StrUtil.isEmpty(paymentId)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "支付ID必须填写");
        }
        SxoPayment payment = this.getById(paymentId);
        if (payment == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "支付ID错误");
        }
        JSONObject object = JSONObject.parseObject(payment.getConfig());
        payment.setAppid(object.getString("appid"));
        payment.setMiniAppid(object.getString("mini_appid"));
        payment.setMchId(object.getString("mch_id"));
        payment.setApiclientCert(object.getString("apiclient_cert"));
        payment.setApiclientKey(object.getString("apiclient_key"));
        if (payment.getPayment() != null && payment.getPayment().equals("Weixin")) {
            payment.setPayChannelType("0001");
        }
        if (payment.getPayment() != null && payment.getPayment().equals("Alipay")) {
            payment.setPayChannelType("0002");
        }
        return payment;
    }
}